package com.taxigang.client.ui.map;

import com.allen_sauer.gwt.log.client.Log;
import com.google.code.gwt.geolocation.client.Coordinates;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.client.uibinder.SearchAndCreate;
import com.taxigang.client.uibinder.TaxiGangMapPanel;
import com.taxigang.shared.TaxiGangInfo;

public class NavigatorManager {
	private static NavigatorWidget navigatorView;


	public static void executeNavigator(final Command cmd, final SearchView searchView, final TaxiGangMapPanel taxiGangMapPanel){
		if (navigatorView == null){
			GWT.runAsync(new RunAsyncCallback() {
				public void onFailure(Throwable caught) {
					Window.alert("Code download failed");
				}

				public void onSuccess() {
					navigatorView = NavigatorWidget.getInstance(searchView);
					navigatorView.showMap(taxiGangMapPanel,cmd);
				}
			});

		} else {
			cmd.execute();
		}
	}

	public static void resetSuggestion(SearchAndCreate searchAndCreate, TaxiGangMapPanel taxiGangMapPanel){
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.resetSuggestion();
				
			}
		};
		executeNavigator(cmd,searchAndCreate,taxiGangMapPanel);
	}
	public static void createSuggestion(final TaxiGangInfo suggestion,
			SearchAndCreate searchAndCreate, TaxiGangMapPanel taxiGangMapPanel,
			boolean b) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.makeSuggestion(suggestion);
				
			}
		};
		executeNavigator(cmd,searchAndCreate,taxiGangMapPanel);
		
	}

	public static void createStartMarker(final String start, SearchView searchView, TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createStartMarker(start,setAddress);
				
			}
		};
		executeNavigator(cmd,searchView,taxiGangMapPanel);
		
	}

	public static void createStartMarker(final Coordinates c, SearchView searchView, TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress, final Command cmd2) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createStartMarker(c,cmd2,setAddress);
				
			}
		};
		executeNavigator(cmd,searchView,taxiGangMapPanel);
		
	}

//	public static void createMarker(final MarkerPos markerPos, SearchView searchView, TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress, Command cmd) {
//		switch (markerPos.type){
//		case ADDR:
//			switch (markerPos.destStart){
//			case DEST:
//				createDestinationMarker(markerPos.address, markerPos.city, searchView, taxiGangMapPanel, setAddress);
//
//				break;
//			case START:
//				createStartMarker(markerPos.address, markerPos.city, searchView, taxiGangMapPanel, setAddress);
//
//				break;
//			}
//
//			break;
//		case COORD:
//			switch (markerPos.destStart){
//			case DEST:
//				throw new RuntimeException("coord destination marker not supported");
//			case START:
//				createStartMarker(markerPos.coords, searchView, taxiGangMapPanel, setAddress, cmd);
//
//				break;
//			}
//		
//			break;
//		}
//	}

	public static void createDestinationMarker(final String destination, SearchView searchView, TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress,final Command payloadCmd) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createDestinationMarker(destination,setAddress,payloadCmd);
				
			}
			
		};
		executeNavigator(cmd,searchView,taxiGangMapPanel);
		
	}

//	public static void createStartMarker(final String startAddress, SearchView searchView, TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress) {
//		Command cmd = new Command(){
//
//			@Override
//			public void execute() {
//				navigatorView.createStartMarker(startAddress,(Command)null,setAddress);
//				
//			}
//			
//		};
//		executeNavigator(cmd,searchView,taxiGangMapPanel);
//		
//	}

	public static void createDestinationMarker(final String destinationAddress, SearchView searchView, TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createDestinationMarker(destinationAddress,(Command)null,setAddress);
				
			}
			
		};
		executeNavigator(cmd,searchView,taxiGangMapPanel);
		
	}

	public static void createStartDestinationMarker(final String startAddress,
			final String destinationAddress, final SearchAndCreate searchView,
			final TaxiGangMapPanel taxiGangMapPanel, final boolean setAddress) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createStartMarker(startAddress,(Command)null,false);
				navigatorView.createDestinationMarker(destinationAddress,(Command)null, setAddress);
				
			}
			
		};
		executeNavigator(cmd,searchView,taxiGangMapPanel);
		
	}

	public static void setUpForCreation(final TaxiGangInfo info, final SearchAndCreate searchView,
			final MapView mapView) {
		Log.info("NavigatorManager:setUpForCreation ");

		final Command startDestAndFindCommand = new Command(){

			@Override
			public void execute() {
				Log.info("cmd:setUpForCreation");
				Command destAndFindCommand = new Command(){

					@Override
					public void execute() {
						Log.info("cmd:destAndSearch");
						
						Command searchCommand = new Command(){
							@Override
							public void execute() {
								Log.info("cmd:searchForTaxiGangs");
								mapView.show(PanelType.MOBILENUMBER);
							}

						};
						
						navigatorView.createDestinationMarker(info.getDestination(),searchCommand,true);


					}};

				navigatorView.createStartMarker(info.getStart(),destAndFindCommand,true);

			}
			
		};
		executeNavigator(startDestAndFindCommand,searchView,null);
		
	}
}
